#pragma once
#include <stdexcept>
#include <string>
#include <map>
#include <vector>
#include <sqlite3.h>

namespace firebird {


class sqlite_db;

class sqlite_table {
public:
	sqlite_table() : row_count_(0), column_count_(0), data_(NULL) {
	}

	~sqlite_table() {
		if (NULL != data_) {
			sqlite3_free_table(data_);
			data_ = NULL;
			row_count_ = 0;
			column_count_ = 0;
		}
	}

	int row_count() {
		return row_count_;
	}

	int column_count() {
		return column_count_;
	}

	char** data() {
		return data_;
	}

private:
	friend class sqlite_db;

	int column_count_;

	int row_count_;

	char** data_;
};

class sqlite_error : public std::runtime_error
{
  public:
    explicit sqlite_error(const std::string& what) : std::runtime_error(what) {
	}
};


typedef std::map<std::string,std::string> sqlite_fields;
typedef std::vector<sqlite_fields> sqlite_result;

class sqlite_db
{
  public:
    sqlite_db():db_(NULL){}
	
    virtual ~sqlite_db() {
		if (db_)
			close();
	}

    void open(const std::string& db);

    void close();

    sqlite_table query(const std::string& sql);

    void exec(const std::string& sql);
  private:
    sqlite3* db_;
};
}